/*
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.apps.mytracks.investigados;

import com.doverty.android.maps.mytracks.R;
import com.google.android.apps.mytracks.AbstractMyTracksActivity;
import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.content.Investigado;
import com.google.android.apps.mytracks.content.TelefonoInvestigado;
import com.google.android.apps.mytracks.fragments.DeleteInvestigadoDialogFragment.DeleteInvestigadoCaller;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

/**
 * An activity to display investigado detail info.
 * 
 * @author Leif Hendrik Wilden
 */
public class InvestigadoDetailActivity extends AbstractMyTracksActivity implements DeleteInvestigadoCaller {

  public static final String EXTRA_INVESTIGADO_ID = "investigado_id";
  private static final String TAG = InvestigadoDetailActivity.class.getSimpleName();

  private Investigado investigado;
  private TelefonoInvestigadoDataSource dbTelefonoInvestigado;
  private InvestigadoDataSource dbInvestigado;
  private List<TelefonoInvestigado> telefonosInvestigado;
  private TextView investigadoNombre;
  private TextView investigadoTelefono1;
  private TextView investigadoTelefonoTipo1;
  private TextView investigadoTelefono2;
  private TextView investigadoTelefonoTipo2;
  private TextView investigadoTelefono3;
  private TextView investigadoTelefonoTipo3;
  private TextView investigadoDireccion;
  private TextView investigadoEmail;


  @Override
  protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    long investigadoId = getIntent().getLongExtra(EXTRA_INVESTIGADO_ID, -1L);
    if (investigadoId == -1L) {
      Log.d(TAG, "invalid investigado id");
      finish();
      return;
    }

    dbInvestigado = new InvestigadoDataSource(this);
    dbInvestigado.open();
    dbTelefonoInvestigado = new TelefonoInvestigadoDataSource(this);
    dbTelefonoInvestigado.open();
    telefonosInvestigado = dbTelefonoInvestigado.getAllTelefonoInvestigado(investigadoId);
    investigado = dbInvestigado.getInvestigado(investigadoId);
    investigadoNombre = (TextView) findViewById(R.id.investigado_detail_nombre_info);
    investigadoNombre.setText(investigado.getNombre());
    if (telefonosInvestigado.size()>0){
      findViewById(R.id.investigado_detail_telefono).setVisibility(View.VISIBLE);
      findViewById(R.id.investigado_detail_telefono_label).setVisibility(View.VISIBLE);
      findViewById(R.id.investigado_detail_telefono_info1).setVisibility(View.VISIBLE);
      telefonosInvestigado = dbTelefonoInvestigado.getAllTelefonoInvestigado(investigadoId);
      investigadoTelefono1 = (TextView) findViewById(R.id.investigado_detail_telefono_num1);
      investigadoTelefono1.setText(telefonosInvestigado.get(0).getNumero());
      investigadoTelefonoTipo1 = (TextView) findViewById(R.id.investigado_detail_telefono_tipo1);
      investigadoTelefonoTipo1.setText(telefonosInvestigado.get(0).getTipo());
      if (telefonosInvestigado.size()>1){
        findViewById(R.id.investigado_detail_telefono_info2).setVisibility(View.VISIBLE);
        investigadoTelefono2 = (TextView) findViewById(R.id.investigado_detail_telefono_num2);
        investigadoTelefono2.setText(telefonosInvestigado.get(1).getNumero());
        investigadoTelefonoTipo2 = (TextView) findViewById(R.id.investigado_detail_telefono_tipo2);
        investigadoTelefonoTipo2.setText(telefonosInvestigado.get(1).getTipo());
        if (telefonosInvestigado.size()>2){
          findViewById(R.id.investigado_detail_telefono_info3).setVisibility(View.VISIBLE);
          investigadoTelefono3 = (TextView) findViewById(R.id.investigado_detail_telefono_num3);
          investigadoTelefono3.setText(telefonosInvestigado.get(2).getNumero());
          investigadoTelefonoTipo3 = (TextView) findViewById(R.id.investigado_detail_telefono_tipo3);
          investigadoTelefonoTipo3.setText(telefonosInvestigado.get(2).getTipo());
        }
      }
    }

    investigadoDireccion = (TextView) findViewById(R.id.investigado_detail_direccion_info);
    investigadoDireccion.setText(investigado.getDireccion());
    investigadoEmail = (TextView) findViewById(R.id.investigado_detail_email_info);
    investigadoEmail.setText(investigado.getEmail());
    
    Button btnRutas = (Button) findViewById(R.id.buttonVerRutasInvestigado);
    btnRutas.setOnClickListener(new View.OnClickListener() {
      @Override
        public void onClick(View v) {
          Intent intent = new Intent(getApplicationContext(), TrackListActivity.class);
          intent.putExtras(getIntent().getExtras());
          startActivity(intent);
        }
    });
  }

  @Override
  protected int getLayoutResId() {
    return R.layout.investigado_detail_activity;
  }

  @Override
  public void onDeleteInvestigadoDone() {
    runOnUiThread(new Runnable() {
        @Override
      public void run() {
        finish();
      }
    });
  }

}
